package com.beemans.corekit.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.anythink.core.api.ATAdInfo;
import com.beemans.corekit.constant.AdvertConstants;
import com.beemans.corekit.databinding.FragmentToponInsertBinding;
import com.beemans.corekit.integration.toolkit.ToastUtils;
import com.beemans.corekit.integration.topon.ATAdInfoExt;
import com.beemans.corekit.integration.topon.InsertAdvert;
import com.beemans.corekit.integration.topon.InsertAdvertCallback;
import com.beemans.corekit.integration.topon.InsertAdvertConfig;
import com.beemans.corekit.ui.dialog.LoadingDialog;
import com.beemans.corekit.utils.TopOnUtils;

public class TopOnInsertFragment extends BaseFragment<FragmentToponInsertBinding> {
    @Nullable
    @Override
    protected FragmentToponInsertBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentToponInsertBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initEvents() {
        binding.insertBtnLoadAd.setOnClickListener(v -> preloadAdvert());

        binding.insertBtnShowAd.setOnClickListener(v -> showAdvert());
    }

    private void preloadAdvert() {
        LoadingDialog.showLoading();

        final InsertAdvertConfig advertConfig = new InsertAdvertConfig(AdvertConstants.TP_INSERT_ID);
        TopOnUtils.loadInsertAdvert(this, advertConfig, getAdvertCallback());
    }

    private void showAdvert() {
        LoadingDialog.showLoading();

        final InsertAdvertConfig advertConfig = new InsertAdvertConfig(AdvertConstants.TP_INSERT_ID);
        advertConfig.setShowAfterLoaded(true);
        advertConfig.setPreloadAdvert(true);

        TopOnUtils.loadInsertAdvert(this, advertConfig, getAdvertCallback());
    }

    @NonNull
    private InsertAdvertCallback getAdvertCallback() {
        return new InsertAdvertCallback() {
            @Override
            public void onAdvertRequestPre(@NonNull InsertAdvert insertAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告开始请求");
            }

            @Override
            public void onAdvertRequestAlt(@NonNull InsertAdvert insertAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告请求成功");
            }

            @Override
            public void onAdvertLoaded(@NonNull InsertAdvert insertAdvert) {
                printLog("广告加载成功");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertLoadFail(@NonNull InsertAdvert insertAdvert, @Nullable String adError) {
                printLog("广告加载失败");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertShow(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告展示 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertShowFail(@NonNull InsertAdvert insertAdvert) {
                printLog("广告展示失败");
            }

            @Override
            public void onAdvertClicked(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告点击 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertClose(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告关闭 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertRevenue(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告收益 --- atAdInfo:" + atAdInfo);
            }
        };
    }

    private void printLog(String msg) {
        Log.e("susu", msg);
        ToastUtils.showShort(msg);
    }
}